﻿using System;
using System.Linq;
using System.Text;
using Omu.AwesomeWebForms;
using Omu.AwesomeWebFormsDemo.Model;
using Omu.AwesomeWebFormsDemo.svc;

namespace Omu.AwesomeWebFormsDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FruitLookup.Value = "3";
                FruitsMultiLookup.Value = new[] { 1, 2, 3 }.ToJson();
                CategoriesMultiLookup.Value = new[] { 1, 2, 3 }.ToJson();
                Category1AjaxDropdown.Value = "1";
                Category2AjaxDropdown.Value = "2";
                CategoriesAjaxRadioList.Value = "3";
                Cat1AjaxCheckboxList.Value = new[] { 1 }.ToJson();
                Cat2AjaxDropdown.Value = "4";
            }
        }

        protected void Show(object sender, EventArgs e)
        {
            var c1 = Aja.Fruits.Single(o => o.Id.ToString() == FruitLookup.Value);

            var fruits = FruitsMultiLookup.Value.GetIntArray();
            var c2 = Aja.Fruits.Where(o => fruits.Contains(o.Id))
                .Select(o => o.Name).ToArray();

            var c3 = Aja.Categories.Single(o => o.Id.ToString() == CategoryAjaxDropdown.Value);
            var c4 = Aja.Fruits.SingleOrDefault(o => o.Id.ToString() == FruitAjaxDropdown.Value) ?? new Fruit { Name = "not selected" };


            var cats = CategoriesMultiLookup.Value.GetIntArray();
            var c5 = Aja.Categories.Where(o => cats.Contains(o.Id))
                .Select(o => o.Name).ToArray();

            var c6 = Aja.Fruits.SingleOrDefault(o => o.Id.ToString() == FruitsFromMultiLookupAjaxDropdown.Value) ?? new Fruit { Name = "not selected" };

            var cats1 = Cat1AjaxCheckboxList.Value.GetIntArray();
            var c7 = Aja.Categories.Where(o => cats1.Contains(o.Id))
                .Select(o => o.Name).ToArray();

            var frts = FruitsFrom2CatAjaxCheckboxList.Value.GetIntArray();
            var c8 = Aja.Fruits.Where(o => frts.Contains(o.Id))
                .Select(o => o.Name).ToArray();

            var c9 = Aja.Fruits.SingleOrDefault(o => o.Id.ToString() == FruitsFrom2CategoriesAjaxDropdown.Value) ?? new Fruit { Name = "not selected" };
            var c10 = Aja.Fruits.SingleOrDefault(o => o.Id.ToString() == FruitsAjaxRadioList.Value) ?? new Fruit { Name = "radiolist not clicked" };

            var s = new StringBuilder();
            s.AppendLine("<h2>values selected:</h2>");
            s.AppendLine("FruitLookup: " + c1.Name);
            s.AppendLine("<br/>FruitsMultiLookup: " + string.Join(",", c2));
            s.AppendLine("<br/>CategoryAjaxDropdown: " + c3.Name);
            s.AppendLine("<br/>FruitAjaxDropdown: " + c4.Name);
            s.AppendLine("<br/>CategoriesMultiLookup: " + string.Join(",", c5));
            s.AppendLine("<br/>FruitsFromMultiLookupAjaxDropdown: " + c6.Name);
            s.AppendLine("<br/>Category1AjaxDropdown: " + Aja.Categories.Single(o => o.Id.ToString() == Category1AjaxDropdown.Value).Name);
            s.AppendLine("<br/>Category2AjaxDropdown: " + Aja.Categories.Single(o => o.Id.ToString() == Category2AjaxDropdown.Value).Name);
            s.AppendLine("<br/>FruitsFrom2CategoriesAjaxDropdown: " + c9.Name);
            s.AppendLine("<br/>CategoriesAjaxRadioList: " + Aja.Categories.Single(o => o.Id.ToString() == CategoriesAjaxRadioList.Value).Name);
            s.AppendLine("<br/>FruitsAjaxRadioList: " + c10.Name);
            s.AppendLine("<br/>Cat1AjaxCheckboxList: " + string.Join(",", c7));
            s.AppendLine("<br/>Cat2AjaxDropdown: " + Aja.Categories.Single(o => o.Id.ToString() == Cat2AjaxDropdown.Value).Name);
            s.AppendLine("<br/>FruitsFrom2CatAjaxCheckboxList: " + string.Join(",", c8));
            s.AppendLine("<br/>FruitsAjaxList at page: " + FruitsAjaxList.PageNumber);

            Result.Text = s.ToString();
        }
    }
}
